﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft
{
    public static class ContentTypes
    {
        private static readonly ContentTypeWithExtension[] cs;
        static ContentTypes()
        {

            cs = init().ToArray();
        }

        public static ContentTypeWithExtension GetByExtension(string ext)
        {
            if (ext != null)
            {
                ext = ext.ToLower();
            }
            foreach (var item in cs)
            {
                if (item.Extension == ext)
                {
                    return item;
                }
            }
            return cs[0];
            // throw new KeyNotFoundException("未找到指定的扩展名。");
        }
        public static ContentTypeWithExtension GetByContentType(string ct)
        {
            if (ct != null)
            {
                ct = ct.ToLower();
            }
            foreach (var item in cs)
            {
                if (item.ContentType == ct)
                {
                    return item;
                }
            }
            throw new KeyNotFoundException("未找到指定的ContentType。");
        }

        private static List<ContentTypeWithExtension> init()
        {

            List<ContentTypeWithExtension> l = new List<ContentTypeWithExtension>(200);

            #region adds

            l.add(null, "application/octet-stream");
            l.add("", "application/octet-stream");
            l.add("*", "application/octet-stream");
            l.add(".*", "application/octet-stream");
            l.add(".001", "application/x-001");
            l.add(".301", "application/x-301");
            l.add(".323", "text/h323");
            l.add(".906", "application/x-906");
            l.add(".907", "drawing/907");
            l.add(".a11", "application/x-a11");
            l.add(".acp", "audio/x-mei-aac");
            l.add(".ai", "application/postscript");
            l.add(".aif", "audio/aiff");
            l.add(".aifc", "audio/aiff");
            l.add(".aiff", "audio/aiff");
            l.add(".anv", "application/x-anv");
            l.add(".asa", "text/asa");
            l.add(".asf", "video/x-ms-asf");
            l.add(".asp", "text/asp");
            l.add(".asx", "video/x-ms-asf");
            l.add(".au", "audio/basic");
            l.add(".avi", "video/avi");
            l.add(".awf", "application/vnd.adobe.workflow");
            l.add(".biz", "text/xml");
            l.add(".bmp", "application/x-bmp");
            l.add(".bot", "application/x-bot");
            l.add(".c4t", "application/x-c4t");
            l.add(".c90", "application/x-c90");
            l.add(".cal", "application/x-cals");
            l.add(".cat", "application/s-pki.seccat");
            l.add(".cdf", "application/x-netcdf");
            l.add(".cdr", "application/x-cdr");
            l.add(".cel", "application/x-cel");
            l.add(".cer", "application/x-x509-ca-cert");
            l.add(".cg4", "application/x-g4");
            l.add(".cgm", "application/x-cgm");
            l.add(".cit", "application/x-cit");
            l.add(".class", "java/*");
            l.add(".cml", "text/xml");
            l.add(".cmp", "application/x-cmp");
            l.add(".cmx", "application/x-cmx");
            l.add(".cot", "application/x-cot");
            l.add(".crl", "application/pkix-crl");
            l.add(".crt", "application/x-x509-ca-cert");
            l.add(".csi", "application/x-csi");
            l.add(".css", "text/css");
            l.add(".cut", "application/x-cut");
            l.add(".dbf", "application/x-dbf");
            l.add(".dbm", "application/x-dbm");
            l.add(".dbx", "application/x-dbx");
            l.add(".dcd", "text/xml");
            l.add(".dcx", "application/x-dcx");
            l.add(".der", "application/x-x509-ca-cert");
            l.add(".dgn", "application/x-dgn");
            l.add(".dib", "application/x-dib");
            l.add(".dll", "application/x-msdownload");
            l.add(".doc", "application/msword");
            l.add(".dot", "application/msword");
            l.add(".drw", "application/x-drw");
            l.add(".dtd", "text/xml");
            l.add(".dwf", "Model/vnd.dwf");
            l.add(".dwf", "application/x-dwf");
            l.add(".dwg", "application/x-dwg");
            l.add(".dxb", "application/x-dxb");
            l.add(".dxf", "application/x-dxf");
            l.add(".edn", "application/vnd.adobe.edn");
            l.add(".emf", "application/x-emf");
            l.add(".eml", "message/rfc822");
            l.add(".ent", "text/xml");
            l.add(".epi", "application/x-epi");
            l.add(".eps", "application/x-ps");
            l.add(".eps", "application/postscript");
            l.add(".etd", "application/x-ebx");
            l.add(".exe", "application/x-msdownload");
            l.add(".fax", "image/fax");
            l.add(".fdf", "application/vnd.fdf");
            l.add(".fif", "application/fractals");
            l.add(".fo", "text/xml");
            l.add(".frm", "application/x-frm");
            l.add(".g4", "application/x-g4");
            l.add(".gbr", "application/x-gbr");
            l.add(".gcd", "application/x-gcd");
            l.add(".gif", "image/gif");
            l.add(".gl2", "application/x-gl2");
            l.add(".gp4", "application/x-gp4");
            l.add(".hgl", "application/x-hgl");
            l.add(".hmr", "application/x-hmr");
            l.add(".hpg", "application/x-hpgl");
            l.add(".hpl", "application/x-hpl");
            l.add(".hqx", "application/mac-binhex40");
            l.add(".hrf", "application/x-hrf");
            l.add(".hta", "application/hta");
            l.add(".htc", "text/x-component");
            l.add(".htm", "text/html");
            l.add(".html", "text/html");
            l.add(".htt", "text/webviewhtml");
            l.add(".htx", "text/html");
            l.add(".icb", "application/x-icb");
            l.add(".ico", "image/x-icon");
            l.add(".ico", "application/x-ico");
            l.add(".iff", "application/x-iff");
            l.add(".ig4", "application/x-g4");
            l.add(".igs", "application/x-igs");
            l.add(".iii", "application/x-iphone");
            l.add(".img", "application/x-img");
            l.add(".ins", "application/x-internet-signup");
            l.add(".isp", "application/x-internet-signup");
            l.add(".IVF", "video/x-ivf");
            l.add(".java", "java/*");
            l.add(".jfif", "image/jpeg");
            l.add(".jpe", "image/jpeg");
            l.add(".jpe", "application/x-jpe");
            l.add(".jpeg", "image/jpeg");
            l.add(".jpg", "image/jpeg");
            l.add(".jpg", "application/x-jpg");
            l.add(".js", "application/x-javascript");
            l.add(".jsp", "text/html");
            l.add(".la1", "audio/x-liquid-file");
            l.add(".lar", "application/x-laplayer-reg");
            l.add(".latex", "application/x-latex");
            l.add(".lavs", "audio/x-liquid-secure");
            l.add(".lbm", "application/x-lbm");
            l.add(".lmsff", "audio/x-la-lms");
            l.add(".ls", "application/x-javascript");
            l.add(".ltr", "application/x-ltr");
            l.add(".m1v", "video/x-mpeg");
            l.add(".m2v", "video/x-mpeg");
            l.add(".m3u", "audio/mpegurl");
            l.add(".m4e", "video/mpeg4");
            l.add(".mac", "application/x-mac");
            l.add(".man", "application/x-troff-man");
            l.add(".math", "text/xml");
            l.add(".mdb", "application/msaccess");
            l.add(".mdb", "application/x-mdb");
            l.add(".mfp", "application/x-shockwave-flash");
            l.add(".mht", "message/rfc822");
            l.add(".mhtml", "message/rfc822");
            l.add(".mi", "application/x-mi");
            l.add(".mid", "audio/mid");
            l.add(".midi", "audio/mid");
            l.add(".mil", "application/x-mil");
            l.add(".mml", "text/xml");
            l.add(".mnd", "audio/x-musicnet-download");
            l.add(".mns", "audio/x-musicnet-stream");
            l.add(".mocha", "application/x-javascript");
            l.add(".movie", "video/x-sgi-movie");
            l.add(".mp1", "audio/mp1");
            l.add(".mp2", "audio/mp2");
            l.add(".mp2v", "video/mpeg");
            l.add(".mp3", "audio/mp3");
            l.add(".mp4", "video/mp4");
            l.add(".mpa", "video/x-mpg");
            l.add(".mpd", "application/-project");
            l.add(".mpe", "video/x-mpeg");
            l.add(".mpeg", "video/mpg");
            l.add(".mpg", "video/mpg");
            l.add(".mpga", "audio/rn-mpeg");
            l.add(".mpp", "application/-project");
            l.add(".mps", "video/x-mpeg");
            l.add(".mpt", "application/-project");
            l.add(".mpv", "video/mpg");
            l.add(".mpv2", "video/mpeg");
            l.add(".mpw", "application/s-project");
            l.add(".mpx", "application/-project");
            l.add(".mtx", "text/xml");
            l.add(".mxp", "application/x-mmxp");
            l.add(".net", "image/pnetvue");
            l.add(".nrf", "application/x-nrf");
            l.add(".nws", "message/rfc822");
            l.add(".odc", "text/x-ms-odc");
            l.add(".out", "application/x-out");
            l.add(".p10", "application/pkcs10");
            l.add(".p12", "application/x-pkcs12");
            l.add(".p7b", "application/x-pkcs7-certificates");
            l.add(".p7c", "application/pkcs7-mime");
            l.add(".p7m", "application/pkcs7-mime");
            l.add(".p7r", "application/x-pkcs7-certreqresp");
            l.add(".p7s", "application/pkcs7-signature");
            l.add(".pc5", "application/x-pc5");
            l.add(".pci", "application/x-pci");
            l.add(".pcl", "application/x-pcl");
            l.add(".pcx", "application/x-pcx");
            l.add(".pdf", "application/pdf");
            l.add(".pdx", "application/vnd.adobe.pdx");
            l.add(".pfx", "application/x-pkcs12");
            l.add(".pgl", "application/x-pgl");
            l.add(".pic", "application/x-pic");
            l.add(".pko", "application-pki.pko");
            l.add(".pl", "application/x-perl");
            l.add(".plg", "text/html");
            l.add(".pls", "audio/scpls");
            l.add(".plt", "application/x-plt");
            l.add(".png", "image/png");
            l.add(".png", "application/x-png");
            l.add(".pot", "application/vnd.ms-powerpoint");
            l.add(".ppa", "application/vs-powerpoint");
            l.add(".ppm", "application/x-ppm");
            l.add(".pps", "application/vnd.ms-powerpoint");
            l.add(".ppt", "application/vnd.ms-powerpoint");
            l.add(".ppt", "application/x-ppt");
            l.add(".pr", "application/x-pr");
            l.add(".prf", "application/pics-rules");
            l.add(".prn", "application/x-prn");
            l.add(".prt", "application/x-prt");
            l.add(".ps", "application/x-ps");
            l.add(".ps", "application/postscript");
            l.add(".ptn", "application/x-ptn");
            l.add(".pwz", "application/powerpoint");
            l.add(".r3t", "text/vnd.rn-realtext3d");
            l.add(".ra", "audio/vnd.rn-realaudio");
            l.add(".ram", "audio/x-pn-realaudio");
            l.add(".ras", "application/x-ras");
            l.add(".rat", "application/rat-file");
            l.add(".rdf", "text/xml");
            l.add(".rec", "application/vnd.rn-recording");
            l.add(".red", "application/x-red");
            l.add(".rgb", "application/x-rgb");
            l.add(".rjs", "application/vnd.rn-realsystem-rjs");
            l.add(".rjt", "application/vnd.rn-realsystem-rjt");
            l.add(".rlc", "application/x-rlc");
            l.add(".rle", "application/x-rle");
            l.add(".rm", "application/vnd.rn-realmedia");
            l.add(".rmf", "application/vnd.adobe.rmf");
            l.add(".rmi", "audio/mid");
            l.add(".rmj", "application/vnd.rn-realsystem-rmj");
            l.add(".rmm", "audio/x-pn-realaudio");
            l.add(".rmp", "application/vnd.rn-rn_music_package");
            l.add(".rms", "application/vnd.rn-realmedia-secure");
            l.add(".rmvb", "application/vnd.rn-realmedia-vbr");
            l.add(".rmx", "application/vnd.rn-realsystem-rmx");
            l.add(".rnx", "application/vnd.rn-realplayer");
            l.add(".rp", "image/vnd.rn-realpix");
            l.add(".rpm", "audio/x-pn-realaudio-plugin");
            l.add(".rsml", "application/vnd.rn-rsml");
            l.add(".rt", "text/vnd.rn-realtext");
            l.add(".rtf", "application/msword");
            l.add(".rtf", "application/x-rtf");
            l.add(".rv", "video/vnd.rn-realvideo");
            l.add(".sam", "application/x-sam");
            l.add(".sat", "application/x-sat");
            l.add(".sdp", "application/sdp");
            l.add(".sdw", "application/x-sdw");
            l.add(".sit", "application/x-stuffit");
            l.add(".slb", "application/x-slb");
            l.add(".sld", "application/x-sld");
            l.add(".slk", "drawing/x-slk");
            l.add(".smi", "application/smil");
            l.add(".smil", "application/smil");
            l.add(".smk", "application/x-smk");
            l.add(".snd", "audio/basic");
            l.add(".sol", "text/plain");
            l.add(".sor", "text/plain");
            l.add(".spc", "application/x-pkcs7-certificates");
            l.add(".spl", "application/futuresplash");
            l.add(".spp", "text/xml");
            l.add(".ssm", "application/streamingmedia");
            l.add(".sst", "application-pki.certstore");
            l.add(".stl", "application/-pki.stl");
            l.add(".stm", "text/html");
            l.add(".sty", "application/x-sty");
            l.add(".svg", "text/xml");
            l.add(".swf", "application/x-shockwave-flash");
            l.add(".tdf", "application/x-tdf");
            l.add(".tg4", "application/x-tg4");
            l.add(".tga", "application/x-tga");
            l.add(".tif", "image/tiff");
            l.add(".tif", "application/x-tif");
            l.add(".tiff", "image/tiff");
            l.add(".tld", "text/xml");
            l.add(".top", "drawing/x-top");
            l.add(".torrent", "application/x-bittorrent");
            l.add(".tsd", "text/xml");
            l.add(".txt", "text/plain");
            l.add(".uin", "application/x-icq");
            l.add(".uls", "text/iuls");
            l.add(".vcf", "text/x-vcard");
            l.add(".vda", "application/x-vda");
            l.add(".vdx", "application/vnd.visio");
            l.add(".vml", "text/xml");
            l.add(".vpg", "application/x-vpeg005");
            l.add(".vsd", "application/vnd.visio");
            l.add(".vsd", "application/x-vsd");
            l.add(".vss", "application/vnd.visio");
            l.add(".vst", "application/vnd.visio");
            l.add(".vst", "application/x-vst");
            l.add(".vsw", "application/vnd.visio");
            l.add(".vsx", "application/vnd.visio");
            l.add(".vtx", "application/vnd.visio");
            l.add(".vxml", "text/xml");
            l.add(".wav", "audio/wav");
            l.add(".wax", "audio/x-ms-wax");
            l.add(".wb1", "application/x-wb1");
            l.add(".wb2", "application/x-wb2");
            l.add(".wb3", "application/x-wb3");
            l.add(".wbmp", "image/vnd.wap.wbmp");
            l.add(".wiz", "application/msword");
            l.add(".wk3", "application/x-wk3");
            l.add(".wk4", "application/x-wk4");
            l.add(".wkq", "application/x-wkq");
            l.add(".wks", "application/x-wks");
            l.add(".wm", "video/x-ms-wm");
            l.add(".wma", "audio/x-ms-wma");
            l.add(".wmd", "application/x-ms-wmd");
            l.add(".wmf", "application/x-wmf");
            l.add(".wml", "text/vnd.wap.wml");
            l.add(".wmv", "video/x-ms-wmv");
            l.add(".wmx", "video/x-ms-wmx");
            l.add(".wmz", "application/x-ms-wmz");
            l.add(".wp6", "application/x-wp6");
            l.add(".wpd", "application/x-wpd");
            l.add(".wpg", "application/x-wpg");
            l.add(".wpl", "application/-wpl");
            l.add(".wq1", "application/x-wq1");
            l.add(".wr1", "application/x-wr1");
            l.add(".wri", "application/x-wri");
            l.add(".wrk", "application/x-wrk");
            l.add(".ws", "application/x-ws");
            l.add(".ws2", "application/x-ws");
            l.add(".wsc", "text/scriptlet");
            l.add(".wsdl", "text/xml");
            l.add(".wvx", "video/x-ms-wvx");
            l.add(".xdp", "application/vnd.adobe.xdp");
            l.add(".xdr", "text/xml");
            l.add(".xfd", "application/vnd.adobe.xfd");
            l.add(".xfdf", "application/vnd.adobe.xfdf");
            l.add(".xhtml", "text/html");
            l.add(".xls", "application/vnd.ms-excel");
            l.add(".xls", "application/x-xls");
            l.add(".xlw", "application/x-xlw");
            l.add(".xml", "text/xml");
            l.add(".xpl", "audio/scpls");
            l.add(".xq", "text/xml");
            l.add(".xql", "text/xml");
            l.add(".xquery", "text/xml");
            l.add(".xsd", "text/xml");
            l.add(".xsl", "text/xml");
            l.add(".xslt", "text/xml");
            l.add(".xwd", "application/x-xwd");
            l.add(".x_b", "application/x-x_b");
            l.add(".x_t", "application/x-x_t");

            #endregion

            l.add(".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.template");
            l.add(".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
            l.add(".ppsx", "application/vnd.openxmlformats-officedocument.presentationml.slideshow");
            l.add(".potx", "application/vnd.openxmlformats-officedocument.presentationml.template");
            l.add(".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            l.add(".xltx", "application/vnd.openxmlformats-officedocument.spreadsheetml.template");
            return l;
        }
        private static void add(this List<ContentTypeWithExtension> list, string ext, string ct)
        {
            list.Add(new ContentTypeWithExtension() { ContentType = ct, Extension = ext });
        }
    }

    public struct ContentTypeWithExtension
    {
        public string Extension { get; set; }

        public string ContentType { get; set; }
    }
}
